package com.neuedu.dao.Imp;

import cn.hutool.core.util.StrUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import com.neuedu.dao.FoodCategoryDao;
import com.neuedu.pojo.FoodCategory;
import com.neuedu.pojo.Shop;
import com.neuedu.utils.JDBCUtil;

import javax.sql.DataSource;
import java.util.List;

public class FoodCategoryDaoImp implements FoodCategoryDao {
    private DataSource dataSource = JDBCUtil.getDruidDataSource();


    // 分页查询
    @Override
    public List<FoodCategory> getFoodCategory(String currentPage, String pageSize, String shopId) throws Exception {
        Integer page = 0;
        Integer size = 0;
        // 判断接收到的数据是否为空
        if(StrUtil.isNotBlank(currentPage)){
            page = Integer.valueOf(currentPage);
        }
        if (StrUtil.isNotBlank(pageSize)){
            size = Integer.parseInt(pageSize);
        }
        // 分页计算公式
        page = (page - 1) * size;
        return Db.use(dataSource).query("select * from food_category where shop_id = ? limit ? , ?",FoodCategory.class,shopId,page,size);
    }



    // 添加数据
    @Override
    public Integer addFoodCategory(String categoryName, String shopId) throws Exception {
        return Db.use(dataSource).insert(
                Entity.create("food_category")
                        .set("category_name",categoryName)
                        .set("shop_id",shopId));
    }


    // 修改数据
    @Override
    public Integer updateFoodCategory(String categoryId,String categoryName, String shopId) throws Exception {
        return Db.use(dataSource).update(
                Entity.create()
                        .set("category_name",categoryName)
                        .set("shop_id",shopId),
                Entity.create("food_category").set("category_id",categoryId)
        );
    }

    // 删除数据
    @Override
    public Integer delFoodCategory(String categoryId) throws Exception {
        return Db.use(dataSource).del(Entity.create("food_category").set("category_id",categoryId));
    }


    // 查询所有数据条数
    @Override
    public Long countFoodCategory(String shopId) throws Exception{
        return Db.use(dataSource).count(Entity.create("food_category").set("shop_id",shopId));
    };
}
